package com.youlai.boot.system.service.order;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.youlai.boot.system.model.entity.LpsWl;
import com.youlai.boot.system.model.vo.CommonOrderVO;
import com.youlai.boot.system.model.vo.LpsOrderCdjgCVO;
import com.youlai.boot.util.MapUtils;
import com.youlai.boot.util.NumberUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class CdjgCOrderHandle {

    public final static String BKS = "BKS";
    public final static String GRP_HQB_CAR = "GRP_HQB_CAR";
    private static final BigDecimal RANGE_0_25 = new BigDecimal(25);
    private static final BigDecimal RANGE_25_45 = new BigDecimal(45);
    private static final BigDecimal RANGE_45_65 = new BigDecimal(65);
    private static final BigDecimal RANGE_65_85 = new BigDecimal(85);
    private static final BigDecimal RANGE_85_170 = new BigDecimal(170);


    private static final Map<String, Integer> BKS_NUM_MAP = new HashMap<>();

    static {
        BKS_NUM_MAP.put("C59501754-001", 40);
        BKS_NUM_MAP.put("C59502053-001", 40);
        BKS_NUM_MAP.put("C59502353-001", 34);
        BKS_NUM_MAP.put("C59502354-001", 34);
    }

    /**
     * @param objList
     * @param lpsWlMap
     * @return
     */
    public List<LpsOrderCdjgCVO> handle(List<CommonOrderVO> objList, Map<String, LpsWl> lpsWlMap) {
        if (CollectionUtil.isEmpty(objList)) {
            return CollectionUtil.newArrayList();
        }
        return objList.stream().map(item -> {
            LpsOrderCdjgCVO order = new LpsOrderCdjgCVO();
            BeanUtils.copyProperties(item, order);
            order.setId(IdUtil.getSnowflakeNextId());
            if (Objects.nonNull(lpsWlMap.get(order.getWl()))) {
                order.setBz(lpsWlMap.get(order.getWl()).getRemark());
            }
            Map<String, String> featureMap = MapUtils.getFeatureMap(item.getTz());
            order.setBks(featureMap.getOrDefault(BKS, ""));
            if (StrUtil.isBlank(order.getBks())) {
                order.setBz("错误:BKS提取数据失败");
                return order;
            }
            order.setCdjgzd(getCdjgzd(order.getBks(), order.getWl()));
            if (StrUtil.isBlank(order.getCdjgzd())) {
                order.setBz("错误:触动机构长度提取数据失败");
                return order;
            }
            order.setGrpHqbCar(featureMap.getOrDefault(GRP_HQB_CAR, ""));
            if (StrUtil.isBlank(order.getGrpHqbCar())) {
                order.setBz("错误:GRP_HQB_CAR提取数据失败");
                return order;
            }
            order.setSldhys(getSldhys(order.getGrpHqbCar()));
            if (StrUtil.isBlank(order.getSldhys())) {
                order.setBz("错误:数量/弹簧颜色提取数据失败");
                return order;
            }
            return order;
        }).toList();
    }

    /**
     * @param bks
     * @param wl
     * @return
     */
    public String getCdjgzd(String bks, String wl) {
        BigDecimal lengthValue = NumberUtils.getLengthValue(bks, BKS_NUM_MAP.get(wl));
        if (Objects.isNull(lengthValue)) {
            return "";
        }
        return lengthValue.toString();
    }

    /**
     * @param grpHqbCar
     * @return
     */
    private String getSldhys(String grpHqbCar) {
        BigDecimal value = NumberUtils.extractValue(grpHqbCar);
        if (Objects.isNull(value)) {
            return "";
        }
        if (value.compareTo(BigDecimal.ZERO) > 0 && value.compareTo(RANGE_0_25) <= 0) {
            return "配一个黑色扎带弹簧";
        } else if (value.compareTo(RANGE_0_25) > 0 && value.compareTo(RANGE_25_45) <= 0) {
            return "配一个蓝色扎带弹簧";
        } else if (value.compareTo(RANGE_25_45) > 0 && value.compareTo(RANGE_45_65) <= 0) {
            return "配一个黄色扎带弹簧";
        } else if (value.compareTo(RANGE_45_65) > 0 && value.compareTo(RANGE_65_85) <= 0) {
            return "配两个蓝色扎带弹簧";
        } else if (value.compareTo(RANGE_65_85) > 0 && value.compareTo(RANGE_85_170) <= 0) {
            return "配两个黄色扎带弹簧";
        } else {
            return "";
        }
    }
}
